import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../view/login/Login'
import Home from "../view/home/Home";
import Welcome from "../view/home/Welcome";
import Users from "../view/user/Users";
import Rights from "../view/power/Rights";
import Roles from "../view/power/Roles";


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
//使用路由导航守卫控制访问权限
router.beforeEach((to, from, next) => {
  // 跳转login直接放行
  if (to.path === '/login') return next()
  // 读取token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 跳转其他页判断token值 不存在直接跳转登录页
  if (!tokenStr) return next('/login')
  // 存在放行
  next()
})

export default router
